package com.ht.demo.service.manager.system.impl;

import com.ht.demo.common.appexception.AppErrorCodeEnum;
import com.ht.demo.common.appexception.AppException;
import com.ht.demo.common.pojo.bo.system.SysMenuBO;
import com.ht.demo.common.pojo.qto.system.SysMenuQTO;
import com.ht.demo.dao.dao.system.SysMenuDao;
import com.ht.demo.dao.dao.system.SysRoleMenuDao;
import com.ht.demo.dao.domain.system.SysMenuDO;
import com.ht.demo.service.convert.system.SysMenuBeanUtil;
import com.ht.demo.service.manager.system.SysMenuManager;
import com.ht.demo.service.manager.system.SysRoleMenuManager;
import com.ht.demo.service.system.UserTokenManager;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class SysMenuManagerImpl implements SysMenuManager {
    @Resource
    private SysMenuDao sysMenuDao;
    @Resource
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    public Boolean insertMenu(SysMenuBO menuBO) throws AppException {
        SysMenuBeanUtil.saveCheckParam(menuBO);
        menuBO.setCreateBy(UserTokenManager.getInstance().getLoginUser().getUserName());
        menuBO.setCreateTime(new Date());

        if (menuBO.getParentId().longValue() != 0L) {
            SysMenuDO parent = sysMenuDao.findById(menuBO.getParentId());
            if (parent == null) {
                throw new AppException("父级菜单不存在，请刷新后重试");
            }
            menuBO.setAncestors(parent.getAncestors() + "," + parent.getId());
        } else {
            menuBO.setAncestors(menuBO.getParentId().toString());
        }

        SysMenuDO menuDO = SysMenuBeanUtil.sysMenuBOToDO(menuBO);
        sysMenuDao.insert(menuDO);

        return Boolean.TRUE;
    }

    @Override
    public Boolean UpdateMenu(SysMenuBO menuBO) throws AppException {
        sysMenuDao.updateById(SysMenuBeanUtil.sysMenuBOToDO(menuBO));
        return Boolean.TRUE;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean delMenu(Long sysMenuId) throws AppException {
        if (sysMenuId == null) {
            return true;
        }
        SysMenuQTO qto = new SysMenuQTO();
        qto.setParentId(sysMenuId);
        Integer count = sysMenuDao.findForPageCount(qto);
        if (count != null && count > 0) {
            throw new AppException("该菜单下存在子菜单不允许删除");
        }
        sysMenuDao.deleteById(sysMenuId);
        sysRoleMenuDao.deleteByMenuId(sysMenuId);

        return Boolean.TRUE;
    }

    @Override
    public List<SysMenuBO> findMenuByIdList(List<Long> menus) throws AppException {
        if (CollectionUtils.isEmpty(menus)) {
            return new ArrayList<>();
        }

        List<SysMenuBO> menuByIds = SysMenuBeanUtil.doList2BOList(sysMenuDao.findMenuByIds(menus));

        return menuByIds;
    }
}
